package com.zhongwang.mms.module.buy.listner;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.mms.common.ImportCallBack;
import com.zhongwang.mms.gen.entity.TDictionary;
import com.zhongwang.mms.gen.entity.TEmploy;
import com.zhongwang.mms.module.buy.dao.ReportPlanDao;
import com.zhongwang.mms.module.buy.model.ExportReportPlanEditLogModel;
import com.zhongwang.mms.module.buy.model.ExportReportPlanEditModel;
import com.zhongwang.mms.module.buy.model.ReportPlanModel;
import com.zhongwang.mms.util.CodeUtil;
import com.zhongwang.mms.util.ShiroUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 提报计划导入 listener
 *
 * @Author: China.wsg
 * @Date: 2020/01/09 16:38
 */
public class ReportPlanEditPlannerListener extends AnalysisEventListener<ExportReportPlanEditModel> {

    private List<TEmploy> employs;
    private ReportPlanDao reportPlanDao;
    private ImportCallBack callBack;
    private List<ExportReportPlanEditLogModel> reportPlanModels = new ArrayList<>();
    private List<Integer> exceptionList = new ArrayList<>();
    private int i = 2;

    public ReportPlanEditPlannerListener(ReportPlanDao reportPlanDao,
                                         ImportCallBack callBack, List<TEmploy> employs
                                         ) {
        this.reportPlanDao = reportPlanDao;
        this.callBack = callBack;
        this.employs = employs;
    }

    @Override
    public void invoke(ExportReportPlanEditModel data, AnalysisContext context) {
        data.setErrorInfo("");
        if (data.getRppId() == null) {
            data.setErrorInfo(data.getErrorInfo()+"ID不可修改！\n");
        }else if (reportPlanDao.getById(data.getRppId())==null){
            data.setErrorInfo(data.getErrorInfo()+"ID不可修改！\n");
        }
        if (data.getRppPlanner() == null) {
            data.setErrorInfo(data.getErrorInfo()+"计划员没填！\n");
        }
        if ( getPlanner(data.getRppPlanner()) == null) {
            data.setErrorInfo(data.getErrorInfo()+"计划员不存在！\n");
        }
        if (data.getRppPlanNo() == null) {
            data.setErrorInfo(data.getErrorInfo()+"计划号没填！\n");
        }
        //if (data.getRppPlanNo() == null) {
        //    data.setErrorInfo(data.getErrorInfo()+"计划号没填！\n");
        //}
        if(data.getErrorInfo().length()==0){
            ReportPlanModel updateParam = new ReportPlanModel();
            updateParam.setRppId(data.getRppId());
            updateParam.setRppPlanner(data.getRppPlanner());
            updateParam.setRppPlanNo(data.getRppPlanNo());
            updateParam.setRppEditFlag(2);
            reportPlanDao.updatePlannerAndPlanNo(updateParam);
        }else {
            reportPlanModels.add(convertObject(data));
        }
        i++;
    }


    /**
     *@name: convertObject
     *@description: 对象类型转换
     *@param: [obj]
     *@returnType: com.zhongwang.mms.module.buy.model.ExportReportPlanEditLogModel
     *@author: 徐一贺
     *@coddingDate: 2020/8/11 8:47
     *@version: v1.0
    **/
    private ExportReportPlanEditLogModel convertObject(ExportReportPlanEditModel obj){
        ExportReportPlanEditLogModel convertObj = new ExportReportPlanEditLogModel();
        convertObj.setRppId(obj.getRppId());
        convertObj.setRppPlanner(obj.getRppPlanner());
        convertObj.setRppPlanNo(obj.getRppPlanNo());
        convertObj.setRppProjectCode(obj.getRppProjectCode());
        convertObj.setRppProjectName(obj.getRppProjectName());
        convertObj.setRppMatDesc(obj.getRppMatDesc());
        convertObj.setRppNum(obj.getRppNum());
        convertObj.setRppUnit(obj.getRppUnit());
        convertObj.setRppTypes(obj.getRppTypes());
        convertObj.setRppPart(obj.getRppPart());
        convertObj.setRppStandardNo(obj.getRppStandardNo());
        convertObj.setRppBrand(obj.getRppBrand());
        convertObj.setRppRemark(obj.getRppRemark());
        convertObj.setRppUseStoreStr(obj.getRppUseStoreStr());
        convertObj.setRppDiagram(obj.getRppDiagram());
        convertObj.setRppAssistType(obj.getRppAssistType());
        convertObj.setRppDesigner(obj.getRppDesigner());
        convertObj.setRppDesignerTel(obj.getRppDesignerTel());
        convertObj.setRppRecTime(obj.getRppRecTime());
        convertObj.setErrorInfo(obj.getErrorInfo());
        convertObj.setErrorDate(new Date());
        convertObj.setErrorUid(ShiroUtils.getUserId());
        return convertObj;
    }


    /** 获取计划员 */
    private TEmploy getPlanner(String planner) {
        for (TEmploy employ : employs) {
            if (employ.getEmpName() != null && employ.getEmpName().equals(planner)) {
                return employ;
            }
        }
        return null;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 异常数据回写行号
        if (exceptionList.size() > 0) {
            ExportReportPlanEditModel model = new ExportReportPlanEditModel();
            model.setRppMatDesc( "第" + exceptionList.toString() + "行数据格式不正确");
            reportPlanModels.add(convertObject(model));
        }
        if (reportPlanModels.size() > 0) {
            callBack.onWarning(reportPlanModels);
        } else {
            callBack.onSuccess();
        }
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) {
        exceptionList.add(i);
        i++;
    }

}